<?php
class ShopModule
{
	public function index()
	{
		global $_FANWE;
		$cache_file = getTplCache('page/shop/shop_index',$_FANWE['request'],2);
		if(getCacheIsUpdate($cache_file,SHARE_CACHE_TIME,1))
		{
			$where = ' WHERE recommend_count > 0';
			$page_args = array();
			$cid = (int)$_FANWE['request']['cid'];
			if($cid > 0 && isset($_FANWE['cache']['shops']['all'][$cid]))
			{
				$cate = $_FANWE['cache']['shops']['all'][$cid];
				$_FANWE['nav_title'] = $cate['name'].$_FANWE['nav_title'];
				if(isset($cate['childs']))
				{
					$cate['childs'][] = $cid;
					$where .= ' AND cate_id IN ('.implode(',',$cate['childs']).')';
				}
				else
					$where .= ' AND cate_id = '.$cid;
				$page_args['cid'] = $cid;
			}
			else
				$cid = 0;
	
			$sql = 'SELECT COUNT(shop_id) FROM '.FDB::table('shop').$where;
			$goods_count = FDB::resultFirst($sql);
			
			$page_size = 20;
			$pager = buildPage('shop/index',$page_args,$goods_count,$_FANWE['page'],$page_size);
			
			$goods_ids = array();
			$sql = 'SELECT * FROM '.FDB::table('shop').$where.' ORDER BY sort ASC,recommend_count DESC,shop_id DESC LIMIT '.$pager['limit'];
			$shop_list = array();
	
			$res = FDB::query($sql);
			while($data = FDB::fetch($res))
			{
				$cache_data = fStripslashes(unserialize($data['data']));
				$data['tags'] = array();
				if($cache_data)
				{
					if($cache_data['tags'] && is_array($cache_data['tags']))
						$data['tags'] = array_slice($cache_data['tags'],0,5);
					
					if($cache_data['goods'] && is_array($cache_data['goods']))
						$data['goods'] = array_slice($cache_data['goods'],0,3);
				}
				unset($data['data']);
				$data['url'] = FU('shop/show',array('id'=>$data['shop_id']));
				$shop_list[$data['shop_id']] = $data;
			}
			
			include template('page/shop/shop_index');
			display($cache_file);
		}
		else
		{
			include $cache_file;
			display();
		}
	}
	
	public function show()
	{
		global $_FANWE;
		$id = (int)$_FANWE['request']['id'];
		if(!$id)
			exit;
		
		$shop = FDB::fetchFirst('SELECT * FROM '.FDB::table('shop').' WHERE shop_id = '.$id);
		if(!$shop)
			fHeader("location: ".FU('shop/index'));
		
		$_FANWE['nav_title'] = $shop['shop_name'].' - '.$_FANWE['nav_title'];
		
		$cache_data = fStripslashes(unserialize($shop['data']));
		$shop['tags'] = $cache_data['tags'];
		
		if(empty($shop['taoke_url']))
			$shop['to_url'] = FU('tgo',array('url'=>$shop['shop_url']));
		else
			$shop['to_url'] = FU('tgo',array('url'=>$shop['taoke_url']));
		
		$page_args['id'] = $id;
		$sql = 'SELECT COUNT(share_id) FROM '.FDB::table('shop_share').' WHERE shop_id = '.$id;
		$share_count = FDB::resultFirst($sql);
		
		$page_size = 20;
		$pager = buildPage('shop/show',$page_args,$share_count,$_FANWE['page'],$page_size);
		
		$share_list = array();
		if($share_count > 0)
		{
			$sql = 'SELECT s.share_id,s.uid,s.content,s.collect_count,s.comment_count,s.create_time,s.cache_data 
				FROM '.FDB::table('shop_share').' AS ss 
				INNER JOIN '.FDB::table('share').' AS s ON s.share_id = ss.share_id 
				WHERE ss.shop_id = '.$id.' ORDER BY ss.share_id DESC LIMIT '.$pager['limit'];
			$share_list = FDB::fetchAll($sql);
			$share_list = FS('Share')->getShareDetailList($share_list,false,true,true,false,0,10);
		}
		
		$shops_tags = FS('Shop')->getUserOtherShopAndTags($id);
		include template('page/shop/shop_show');
		display();
	}
	
public function dztindex()
	{
		global $_FANWE;
		
		$where = ' WHERE recommend_count > 0 and is_show=1 ';
		//$where = ' WHERE recommend_count > 0';
		$page_args = array();
		$cid = (int)$_FANWE['request']['cid'];
		if($cid > 0 && isset($_FANWE['cache']['shops']['all'][$cid]))
		{
			$cate = $_FANWE['cache']['shops']['all'][$cid];
			$_FANWE['nav_title'] = $cate['name'].$_FANWE['nav_title'];
			if(isset($cate['childs']))
			{
				$cate['childs'][] = $cid;
				$where .= ' AND cate_id IN ('.implode(',',$cate['childs']).')';
			}
			else
				$where .= ' AND cate_id = '.$cid;
			$page_args['cid'] = $cid;
		}
		else
			$cid = 0;

		$sql = 'SELECT COUNT(shop_id) FROM (select shop_id from '.FDB::table('shop').$where.' group by shop_name) as a';
		$goods_count = FDB::resultFirst($sql);
		
		$page_size = 8;
		$pager = buildPage('shop/index',$page_args,$goods_count,$_FANWE['page'],$page_size);
		
		
		$sql = 'SELECT * FROM '.FDB::table('shop').$where.' ORDER BY sort ASC,recommend_count DESC,shop_id DESC LIMIT '.$pager['limit'];
		$shop_list = array();
		$res = FDB::query($sql);
		while($data = FDB::fetch($res))
		{
			$shop_name_main=$data['shop_name'];
			$sub_sql="select * from ".FDB::table('shop')." where shop_name = '".$shop_name_main."'";
			$sub_res = FDB::query($sub_sql);
			$data['shop_url_list']=array();
			$data['coupon']=array();
			$data['activity']=array();
			$shop_ids = array();
			while($sub_data=FDB::fetch($sub_res)){
				if(stristr($sub_data['shop_url'],'taobao.com')){
					if(empty($sub_data['taoke_url'])){
						$data['shop_url_list']['taobao'] =$sub_data['shop_url'];
					}else{
						$data['shop_url_list']['taobao'] =$sub_data['taoke_url'];
					}			
				}elseif (stristr($sub_data['shop_url'],'paipai.com')){
					if(empty($sub_data['taoke_url'])){
						$data['shop_url_list']['paipai'] = $sub_data['shop_url'];
					}else{
						$data['shop_url_list']['paipai'] = $sub_data['taoke_url'];
					}
				}elseif (stristr($sub_data['shop_url'],'dangdang.com')){
					if(empty($sub_data['taoke_url'])){
						$data['shop_url_list']['dangdang'] = $sub_data['shop_url'];
					}else{
						$data['shop_url_list']['dangdang'] = $sub_data['taoke_url'];
					}
				}
				$coupon=array();
				if(null!=$sub_data['coupon']&&""!=$sub_data['coupon']){
					$coupon['c']=$sub_data['coupon'];
					if(null!=$sub_data['coupon_pic']&&""!=$sub_data['coupon_pic']){
						$coupon['p']=$sub_data['coupon_pic'];
					}
					
					$data['coupon'][]=$coupon;
				}
				
				if(null!=$sub_data['activity']&&""!=$sub_data['activity']){
					$data['activity'][]=$sub_data['activity'];
				}
				$shop_ids[]=$sub_data['shop_id'];
							
			}
			

			$data['url'] = FU('shop/show',array('id'=>$data['shop_id']));
			$shop_list[$data['shop_id']] = $data;
				$sql_condition=" 1 ";
				if(count($shop_ids)>1){
					$shop_ids_imp = implode(',',$shop_ids);
					$sql_condition=' sg.shop_id in('.$shop_ids_imp.')';
				}
				if(count($shop_ids)==1){
					$sql_condition=' sg.shop_id = '.$shop_ids[0];
				}
/*				$goods_sql = 'SELECT distinct share_id FROM '.FDB::table('share_goods').' WHERE goods_id IN ('.$goods_ids_imp.') ORDER BY goods_id DESC limit 0,5';
				$shareid_res=FDB::query($goods_sql);
				$shareId_res_arr=array();
				while($share_ids_str=FDB::fetch($shareid_res)){
					$shareId_res_arr[]=$share_ids_str['share_id'];
				}
				$share_ids= implode(',',$shareId_res_arr);
				$share_sql='SELECT * FROM '.FDB::table('share').' WHERE share_id IN ('.$share_ids.') ORDER BY share_id DESC';*/
				//$share_sql='select * from '.FDB::table('share').' as s inner join '.FDB::table('share_goods').' as sg on s.share_id=sg.share_id where '.$sql_condition.' and s.is_index=1 order by s.share_id desc, s.sort asc limit 0,5';
				$share_sql='select * from '.FDB::table('share').' as s inner join '.FDB::table('share_goods').' as sg on s.share_id=sg.share_id where '.$sql_condition.' order by s.share_id desc, s.sort asc limit 0,5';
				$s['share_item_list']=FDB::fetchAll($share_sql);
				$s['share_item_list']=FS("Share")->getShareDetailList($s['share_item_list']);
				$shop_list[$data['shop_id']]['items']=$s['share_item_list'];
				

						
		}
			
		
		include template('page/shop/dzt_shop_index');
		display();
	}
	
public function dztshow()
	{
		global $_FANWE;
		$id = (int)$_FANWE['request']['id'];
		if(!$id)
			exit;
		///////////////////////////////////////////////////////////////
		$category = urldecode($_FANWE['request']['cate']);
		$is_root = false;
			$page_args = array();
			$page_args['id']=$id;
			if(isset($_FANWE['cache']['goods_category']['cate_code'][$category]))
			{
				$page_args['cate'] = $_FANWE['request']['cate'];
				$cate_id = $_FANWE['cache']['goods_category']['cate_code'][$category];
				$goods_cate_code = $category;
			}
			else
			{
				$is_root = true;
				$cate_id = $_FANWE['cache']['goods_category']['root'];
			}
	
			$sort = $_FANWE['request']['sort'];
			//update default sort from pop to new by op 20120225
			$sort = !empty($sort) ? $sort : "new";
	
			$category_data = $_FANWE['cache']['goods_category']['all'][$cate_id];
			$category_tags = array();
	
			$_FANWE['nav_title'] = $category_data['cate_name'];
			
			
			if(!$is_root)
			{
				$cids = array();
				FS('Share')->getChildCids($cate_id,$cids);
				$condition = " AND sc.cate_id IN (".implode(',',$cids).")";
			}

	
			//输出排序URL
			$sort_page_args = $page_args;
			$sort_page_args['sort'] = 'hot7';
	
			$hot7_url['url'] = FU('shop/'.ACTION_NAME,$sort_page_args);
			if($sort=='hot7')
				$hot7_url['act'] = 1;
	
			$sort_page_args['sort'] = 'hot30';
			$hot30_url['url'] = FU('shop/'.ACTION_NAME,$sort_page_args);
			if($sort=='hot30')
				$hot30_url['act'] = 1;
	
			$sort_page_args['sort'] = 'new';
			$new_url['url'] = FU('shop/'.ACTION_NAME,$sort_page_args);
			if($sort=='new')
				$new_url['act'] = 1;
	
			$sort_page_args['sort'] = 'pop';
			$pop_url['url'] = FU('shop/'.ACTION_NAME,$sort_page_args);
			if($sort=='pop')
				$pop_url['act'] = 1;
	
			if(!empty($_FANWE['request']['sort']))
				$page_args['sort'] = $sort;
			else
				$page_args['sort'] = 'new';
	
			$today_time = getTodayTime();
			switch($sort)
			{
				//7天最热 点击次数
				case 'hot7':
					$day7_time = $today_time - 604800;
					$field = ",(s.create_time > $day7_time) AS time_sort ";
					$sort = " ORDER BY time_sort DESC,s.click_count DESC";
				break;
				//30天最热 点击次数
				case 'hot30':
					$day30_time = $today_time - 2592000;
					$field = ",(s.create_time > $day30_time) AS time_sort ";
					$sort = " ORDER BY time_sort DESC,s.click_count DESC";
				break;
				//最新
				case 'new':
					$field = '';
					$sort = " ORDER BY s.share_id DESC";
				break;
				//潮流  喜欢人数
				case 'pop':
				default:
					$day7_time = $today_time - 604800;
					$field = ",(s.create_time > $day7_time) AS time_sort ";
					$sort = " ORDER BY time_sort DESC,s.collect_count DESC";
				break;
			}
		///////////////////////////////////////////////////////////////
		$shop = FDB::fetchFirst('SELECT * FROM '.FDB::table('shop').' WHERE shop_id = '.$id);
		
		$shop['dzt_url']=FU('shop/index',array('id'=>$id));
		if(!$shop)
			fHeader("location: ".FU('shop/index'));
		
		$_FANWE['nav_title'] = $shop['shop_name'].' - '.$_FANWE['nav_title'];
		
		$shop_list=FDB::fetchAll("select * from ".FDB::table('shop')." WHERE shop_name ='".$shop['shop_name']."'");
		
		$shop_ids='';
		
		foreach ($shop_list as $key=>$shop_info){
			
			if(stristr($shop_info['shop_url'],'taobao.com')){				
				if(empty($shop_info['taoke_url'])){
					$shop['shop_url_list']['taobao'] =$shop_info['shop_url'];
				}else{
					$shop['shop_url_list']['taobao'] =$shop_info['taoke_url'];
				}				
			}elseif (stristr($shop_info['shop_url'],'paipai.com')){
				if(empty($shop_info['taoke_url'])){
					$shop['shop_url_list']['paipai'] =$shop_info['shop_url'];
				}else{
					$shop['shop_url_list']['paipai'] =$shop_info['taoke_url'];
				}
			}elseif (stristr($shop_info['shop_url'],'dangdang.com')){
				if(empty($shop_info['taoke_url'])){
					$shop['shop_url_list']['dangdang'] =$shop_info['shop_url'];
				}else{
					$shop['shop_url_list']['dangdang'] =$shop_info['taoke_url'];
				}
			}
			
			if($shop_info[coupon]&&$shop_info[coupon_pic]){	 
				$yhq[$key][coupon]=$shop_info[coupon];
			  	$yhq[$key][coupon_pic]=$shop_info[coupon_pic];
		    }
			if($shop_info[activity ]){
				$hdsm[$key]=$shop_info[activity ];
			}
			
			$shop_ids=$shop_info['shop_id'].','.$shop_ids;
		}
		$shop['shop_ids']=substr($shop_ids,0,-1);
		
		
		
		$cache_data = fStripslashes(unserialize($shop['data']));
		$shop['tags'] = $cache_data['tags'];
		
		if(empty($shop['taoke_url']))
			$shop['to_url'] = FU('tgo',array('url'=>$shop['shop_url']));
		else
			$shop['to_url'] = FU('tgo',array('url'=>$shop['taoke_url']));
		
		$page_args['id'] = $id;
		if(!$is_root){
			$sql='SELECT COUNT(DISTINCT sg.share_id) FROM '.FDB::table('share_goods').' AS sg 
			INNER JOIN '.FDB::table('share').' AS s ON s.share_id = sg.share_id 
			INNER JOIN '.FDB::table('share_category').' AS sc ON sg.share_id = sc.share_id 
			WHERE s.status = 1 AND sg.shop_id in ('.$shop['shop_ids'].') '.$condition;
		}else{
			$sql = 'SELECT COUNT(DISTINCT sg.share_id) FROM '.FDB::table('share_goods').' AS sg 
			INNER JOIN '.FDB::table('share').' AS s ON s.share_id = sg.share_id 
			WHERE s.status = 1 AND sg.shop_id in ('.$shop['shop_ids'].')';
		}
		$share_count = FDB::resultFirst($sql);
		
		$page_size = 18;
		$pager = buildPage('shop/show',$page_args,$share_count,$_FANWE['page'],$page_size);
		
		$share_ids = array();
		if(!$is_root){
			$sql='SELECT DISTINCT sg.share_id '.$field.' FROM '.FDB::table('share_goods').' AS sg 
			INNER JOIN '.FDB::table('share').' AS s ON s.share_id = sg.share_id 
			INNER JOIN '.FDB::table('share_category').' AS sc ON sg.share_id = sc.share_id 
			WHERE s.status = 1 AND sg.shop_id in ('.$shop['shop_ids'].') '.$condition.$sort.' LIMIT '.$pager['limit'];
		}else{
			$sql = 'SELECT DISTINCT sg.share_id '.$field.' FROM '.FDB::table('share_goods').' AS sg 
			INNER JOIN '.FDB::table('share').' AS s ON s.share_id = sg.share_id 
			WHERE s.status = 1 AND sg.shop_id in ('.$shop['shop_ids'].') '.$sort.' LIMIT '.$pager['limit'];
		}
		$res = FDB::query($sql);
		while($data = FDB::fetch($res))
		{
			$share_ids[] = $data['share_id'];
		}
		
		if(isset($_FANWE['request']['ood'])){
			$user_click_shareid=$_FANWE['request']['ood'];
			if(!in_array($user_click_shareid, $share_ids)){
				$share_ids[]=$user_click_shareid;
			}
		}
				
		$share_list = array();
		if(count($share_ids) > 0)
		{
			$share_ids = implode(',',$share_ids);
			$sql = 'SELECT * '.$field.' FROM '.FDB::table('share').' as s WHERE share_id IN ('.$share_ids.')'.$sort;
			$share_list = FDB::fetchAll($sql);
			if(isset($_FANWE['request']['ood'])){
				for($i=0;$i<count($share_list);$i++){
					if($share_list[$i]['share_id']==$user_click_shareid){
						$share_temp=$share_list[0];
						$share_list[0]=$share_list[$i];
						$share_list[$i]=$share_temp;
						break;
					}
				}
				$share_list=array_slice($share_list,0,18);			
			}
			$share_list = FS('Share')->getShareDetailList($share_list,false,true,true,false,0,10);
		}
		
		$shops_tags = FS('Shop')->getUserOtherShopAndTags($id);
		include template('page/shop/dzt_shop_show');
		display();
	}
	
	
	
	
	
	
	
}
?>